package com.android.aishangauction.activity.mine;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.activity.BaseObject;
import com.android.aishangauction.app.BaseActivity;
import com.android.aishangauction.model.member.SignInData;
import com.android.aishangauction.model.member.SignInDataCallback;
import com.android.aishangauction.model.member.SignInRCData;
import com.android.aishangauction.model.member.SignInRCDataCallback;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * Created by Administrator on 2017/9/7.
 */

public class ScoreSignInActivity extends BaseActivity {


    @BindView(R.id.main_top_back)
    ImageView back;
    @BindView(R.id.main_top_scoredetail)
    TextView main_top_scoredetail;
    @BindView(R.id.sign_in_score)
    TextView sign_in_score;
    @BindView(R.id.signin_date)
    TextView signin_date;
    @BindView(R.id.sign_in_score_add)
    TextView sign_in_score_add;
    @BindView(R.id.sign_in_progress)
    ImageView sign_in_progress;
    @BindView(R.id.sign_in_rightnow)
    ImageView sign_in_rightnow;
    @BindView(R.id.sign_in)
    ImageView sign_in;
    @BindView(R.id.scorein_explain_tv)
    TextView scoreExplain;
    private boolean networkConnected;
    private Activity mcontext;
    private String score;
    private String score_left;
    private int signed;
    private String rule;
    private String explain;
    private String continue_day;
    AlertDialog dialog;
    AlertDialog successDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoresignin);
        mcontext=this;
        ButterKnife.bind(this);
        getData();

    }
    private void initUI() {
        sign_in_score.setText(score_left);
        sign_in_score_add.setText("+"+score+"积分");
        if(signed==1)
        {
            signin_date.setText("明日签到");
            sign_in_rightnow.setVisibility(View.GONE);
            sign_in.setVisibility(View.VISIBLE);
        }
        else
        {
            signin_date.setText("今日签到");
            sign_in_rightnow.setVisibility(View.VISIBLE);
            sign_in.setVisibility(View.GONE);
        }

     switch (continue_day)
     {
         case "1":
             sign_in_progress.setImageResource(R.mipmap.sign_progress1);
             break;
         case "2":
             sign_in_progress.setImageResource(R.mipmap.sign_progress2);
             break;
         case "3":
             sign_in_progress.setImageResource(R.mipmap.sign_progress3);
             break;
         case "4":
             sign_in_progress.setImageResource(R.mipmap.sign_progress4);
             break;
         case "5":
             sign_in_progress.setImageResource(R.mipmap.sign_progress5);
             break;
         case "6":
             sign_in_progress.setImageResource(R.mipmap.sign_progress6);
             break;
         case "7":
             sign_in_progress.setImageResource(R.mipmap.sign_progress7);
             break;
     }

        scoreExplain.setText(Html.fromHtml(explain));
    }

    @OnClick({R.id.main_top_back,R.id.sign_in_rightnow, R.id.main_top_scoredetail, R.id.signin_rule_ll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_top_back:
                finish();
                break;
            case R.id.sign_in_rightnow:
                toSign();
                break;
            case R.id.signin_rule_ll:
                showRuleDialog();
                break;
            case R.id.main_top_scoredetail:
                Intent intent_score = new Intent(mcontext, BaseObject.class);
                intent_score.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "积分明细");
                startActivity(intent_score);
                break;
        }
    }
    //签到
    private void toSign() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERFSIGNACTION;
        MyLog.e("地址", address);
        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, null, signInCallback);
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    SignInDataCallback signInCallback = new SignInDataCallback() {
        @Override
        public void onError(Call call, Exception e, int id) {
        }

        @Override
        public void onResponse(SignInData response, int id) {
            if(response.getMsg().equals("签到成功")) {
                score = response.getData().getScore();
                continue_day = response.getData().getContinue();
                showSuccessDialog();
            }
        }
    };

    //获取签到数据
    private void getData() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERFSIGN;
        MyLog.e("地址", address);
        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, null, dataCallback);
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    SignInRCDataCallback dataCallback = new SignInRCDataCallback() {
        @Override
        public void onError(Call call, Exception e, int id) {
        }

        @Override
        public void onResponse(SignInRCData response, int id) {
            if(response.getCode().equals("0")) {
                score= response.getData().getToday_sign_score();
                score_left=response.getData().getTotal_left();
                continue_day = response.getData().getContinue_sign();
                signed = response.getData().getSigned();
                rule = response.getData().getRule();
                explain = response.getData().getExplain();
                initUI();
            }
        }
    };

    private void showSuccessDialog() {
        successDialog = new AlertDialog.Builder(mcontext).create();
        successDialog.getWindow().setDimAmount(0.5f);//设置昏暗度为0
        successDialog.setCanceledOnTouchOutside(false);
        successDialog.show();

        LayoutInflater inflater = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_sign_success, null);
        TextView sure = (TextView) v.findViewById(R.id.dialog_sure);
        TextView tvTodatScore = (TextView) v.findViewById(R.id.dialog_tv_score_num);
        TextView tvContinueDay = (TextView) v.findViewById(R.id.dialog_tv_sign_day);
        SpannableString spannableString  = new SpannableString("获得 " + score + " 积分");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(colorSpan, 3, 3+score.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvTodatScore.setText(spannableString);
        tvContinueDay.setText("已连续签到"+continue_day+"天");
        sure.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        successDialog.dismiss();
                                        getData();
                                    }
                                }
        );
        successDialog.getWindow().setContentView(v);
    }

    private void showRuleDialog() {
        LayoutInflater inflater = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_signin_rule, null);
        TextView sure = (TextView) v.findViewById(R.id.dialog_sure);
        TextView content = (TextView) v.findViewById(R.id.dialog_content);
        content.setText(Html.fromHtml(rule));
        sure.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        dialog.dismiss();
                                    }
                                }
        );
        dialog = new AlertDialog.Builder(mcontext).setView(v).setCancelable(true).show();
    }
}
